﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditHelpOnline : System.Web.UI.UserControl
    {
        public int id;
        public string name;
        public string image;
        public string email;
        public string phone;
        public string filePathNewsIamge;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_HelpOnline", "HelpOnline_ID", id.ToString());
                name = dt.Rows[0]["HelpOnline_Name"].ToString();
                image = dt.Rows[0]["HelpOnline_Image"].ToString();
                email = dt.Rows[0]["HelpOnline_Yahoo"].ToString();
                phone = dt.Rows[0]["HelpOnline_Sdt"].ToString();
                txtName.Text = name;
                txtEmail.Text = email;
                txtPhone.Text = phone;
            }
            catch (Exception)
            {
                Response.Redirect("/Admin/error.htm");
            }
        }

        protected void btnUpdateNews_Click(object sender, EventArgs e)
        {
            if (txtName.Text != "")
            {
                if (txtEmail.Text != "")
                {
                    if (txtPhone.Text != "")
                    {
                        if (fulImageNews.HasFile)
                        {
                            if (LIB.Validate.CheckFileType(fulImageNews.FileName))
                            {
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathNewsIamge = "/Resources/Images/Images-HelpOnline/" + fulImageNews.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-HelpOnline/"), fulImageNews.FileName, 200, fulImageNews.PostedFile.InputStream);
                                //File.Delete(MapPath(image));
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                                cl[0] = new DAL.DHelpers.Column("HelpOnline_Image", filePathNewsIamge, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[1] = new DAL.DHelpers.Column("HelpOnline_Name", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[2] = new DAL.DHelpers.Column("HelpOnline_Yahoo", txtEmail.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[3] = new DAL.DHelpers.Column("HelpOnline_Sdt", txtPhone.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                

                                string[] colWhere = new string[1];
                                colWhere[0] = "HelpOnline_ID";

                                object[] colValue = new object[1];
                                colValue[0] = id;

                                DAL.DCollector.DBCollector.Instance.Update("tbl_HelpOnline", cl, colWhere, colValue);
                                Response.Redirect("/Admin/Index.aspx?panel=7&sub=2");
                            }
                            else
                            {
                                Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                            }
                        }
                        else
                        {
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                                cl[0] = new DAL.DHelpers.Column("HelpOnline_Name", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[1] = new DAL.DHelpers.Column("HelpOnline_Yahoo", txtEmail.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[2] = new DAL.DHelpers.Column("HelpOnline_Sdt", txtPhone.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            string[] colWhere = new string[1];
                            colWhere[0] = "HelpOnline_ID";

                            object[] colValue = new object[1];
                            colValue[0] = id;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_HelpOnline", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=7&sub=2");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Bạn vui lòng nhập số điện thoại người trợ giúp')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Bạn vui lòng nhập email người trợ giúp')</script>");
                }

            }
            else
            {
                Response.Write("<script>alert('Bạn vui lòng nhập tên người trợ giúp online')</script>");
            }
        }
    }
}